/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.WebConfig;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.dao.WebConfigMapper;
import com.yami.shop.service.WebConfigService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author SJL
 * @date 2021-02-20 09:44:42
 */
@Service
@AllArgsConstructor
public class WebConfigServiceImpl extends ServiceImpl<WebConfigMapper, WebConfig> implements WebConfigService {

    private final WebConfigMapper webConfigMapper;
    private final ShopConfig shopConfig;

    @Override
    @Cacheable(cacheNames = "ActivityWebConfig", key = "#configType")
    public WebConfig getActivityWebConfig(Integer configType) {
        WebConfig webConfig = getOne(new LambdaQueryWrapper<WebConfig>()
                .eq(WebConfig::getIsActivity, 1)
                .eq(WebConfig::getConfigType, configType));
        if (webConfig != null) {
            spliceImgPre(webConfig);
        }
        return webConfig;
    }

    @Override
    @CacheEvict(cacheNames = "ActivityWebConfig", key = "#configType")
    public void removeActivityWebConfig(Integer configType) {
    }

    /**
     * 根据configType分页获取不同端的全部配置信息
     *
     * @param page
     * @param webConfig
     * @return
     */
    @Override
    public PageParam<WebConfig> getPageByConfigType(PageParam<WebConfig> page, WebConfig webConfig) {

        PageParam<WebConfig> result = page(page, new LambdaQueryWrapper<WebConfig>()
                .eq(WebConfig::getConfigType, webConfig.getConfigType()));

        List<WebConfig> webConfigList = result.getRecords();
        //无配置信息
        if (webConfigList.isEmpty()) {
            return null;
        }
        //给图片拼接资源域名前缀
        for (WebConfig item : webConfigList) {
            spliceImgPre(item);
        }

        return result;
    }

    /**
     * 增加图片资源前缀
     *
     * @param webConfig
     * @return
     */
    private void spliceImgPre(WebConfig webConfig) {
        String resourcesUrl = shopConfig.getDomain().getResourcesDomainName() + "/";

        if (webConfig.getConfigType() == 1 || webConfig.getConfigType() == 4) {
            // 后台管理的图片
            webConfig.setBsLoginBgImg(StringUtils.isEmpty(webConfig.getBsLoginBgImg()) ? null : resourcesUrl + webConfig.getBsLoginBgImg());
            webConfig.setBsLoginLogoImg(StringUtils.isEmpty(webConfig.getBsLoginLogoImg()) ? null : resourcesUrl + webConfig.getBsLoginLogoImg());
            webConfig.setBsTitleImg(StringUtils.isEmpty(webConfig.getBsTitleImg()) ? null : resourcesUrl + webConfig.getBsTitleImg());
        } else if (webConfig.getConfigType() == 2) {
            // PC端的图片
            webConfig.setPcLogoImg(StringUtils.isEmpty(webConfig.getPcLogoImg()) ? null : resourcesUrl + webConfig.getPcLogoImg());
            webConfig.setPcLogoImgText(StringUtils.isEmpty(webConfig.getPcLogoImgText()) ? null : resourcesUrl + webConfig.getPcLogoImgText());
            webConfig.setPcTitleImg(StringUtils.isEmpty(webConfig.getPcTitleImg()) ? null : resourcesUrl + webConfig.getPcTitleImg());
            webConfig.setPcQrcodeImg(StringUtils.isEmpty(webConfig.getPcQrcodeImg()) ? null : resourcesUrl + webConfig.getPcQrcodeImg());
            webConfig.setPcLoginBgImg(StringUtils.isEmpty(webConfig.getPcLoginBgImg()) ? null : resourcesUrl + webConfig.getPcLoginBgImg());

        } else if (webConfig.getConfigType() == 3) {
            // H5端的图片
            webConfig.setUniLoginLogoImg(StringUtils.isEmpty(webConfig.getUniLoginLogoImg()) ? null : resourcesUrl + webConfig.getUniLoginLogoImg());
        }
    }
}
